package model

import "gorm.io/gorm"

type Submit struct {
	gorm.Model
	Identity  string   `gorm:"column:identity" json:"identity"`
	ProblemId string   `gorm:"column:problem_id" json:"problem_id"`
	Problem   *Problem `gorm:"foreignKey:ProblemId;references:ID" json:"problem"`
	UserId    string   `gorm:"column:user_id" json:"user_id"`
	User      *User    `gorm:"foreignKey:UserId;references:ID" json:"user"`
	Status    int      `gorm:"column:status;type:int;" json:"status"`
	Path      string   `gorm:"column:path;type:varchar(255);" json:"path"`
}

func (table *Submit) TableName() string {
	return "submit"
}

func GetSubmitList(problem_id, user_id string, status int) *gorm.DB {
	tx := DB.Model(new(Submit)).Preload("Problem").Preload("User")
	if problem_id != "" {
		tx = tx.Where("problem_id = ?", problem_id)
	}
	if user_id != "" {
		tx = tx.Where("user_id = ?", user_id)
	}
	if status != 0 {
		tx = tx.Where("status = ?", status)
	}
	return tx
}
